/**
 * 
 */
package com.gosophia.conference.action.starting;

import java.util.ArrayList;
import java.util.List;

import net.sf.json.JSONArray;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.security.context.SecurityContextHolder;
import org.springframework.stereotype.Controller;

import com.gosophia.commons.web.action.BaseSupportAction;
import com.gosophia.conferenceManagement.starting.dto.ConferenceStartingDTO;
import com.gosophia.conferenceManagement.starting.ws.ConferenceTransientStoreWebService;
import com.gosophia.oa.action.JudgeCurrentUndertaker;
import com.gosophia.reportData.bean.UserInfoBean;
import com.gosophia.springsecurity.UserDetailsImpl;
import com.gosophia.userManagement.dto.UserInfoDTO;
import com.gosophia.userManagement.ws.UserManagementWebService;

/**
 * 功能：会议申请第2页，当点击下一步时触发的动作
 * 
 * @author tianchunyu
 * @since 2011-3-22 Copyright (c) 2010 Gosophia corporation All Rights Reserved
 */
@Controller("ConferenceStarting_2_Action")
@Scope("prototype")
public class ConferenceStarting_2_Action extends BaseSupportAction {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	@Autowired
	private ConferenceTransientStoreWebService conferenceTransientStoreWeService;
	@Autowired
	private UserManagementWebService userManagementWebService;
	@Autowired
	private JudgeCurrentUndertaker judgeCurrentUndertaker;
	private ConferenceStartingDTO conferenceStartingDTO;
	private String userInfoDtosJson;

	@Override
	public String _execute() throws Exception {
		// 得到当前用户
		UserDetailsImpl userDetails = (UserDetailsImpl) SecurityContextHolder
				.getContext().getAuthentication().getPrincipal();
		Long creatorId = userDetails.getUserAccountDTO().getUserInfoDTO()
				.getUserInfoId();
		if (conferenceStartingDTO != null
				&& conferenceStartingDTO.getJobId() != null) {
			judgeCurrentUndertaker
					.judgeOperationAuthority(conferenceStartingDTO.getJobId());
		}

		List<UserInfoDTO> userInfoDtos = userManagementWebService
				.findAllUserInfoHasAccount();
		if (userInfoDtos != null) {
			List<UserInfoBean> userInfoBeans = new ArrayList<UserInfoBean>();
			for (UserInfoDTO userInfoDto : userInfoDtos) {
				// if (creatorId.equals(userInfoDto.getUserInfoId())) {
				// continue;
				// }
				UserInfoBean userInfoBean = new UserInfoBean();
				userInfoBean.setId(userInfoDto.getUserInfoId());
				userInfoBean.setName(userInfoDto.getUserName());
				userInfoBean.setType("acc");
				userInfoBeans.add(userInfoBean);
			}
			userInfoDtosJson = JSONArray.fromObject(userInfoBeans).toString()
					.replace("\"", "\\\"");
		}
		conferenceStartingDTO = conferenceTransientStoreWeService
				.updateTransientInfo(conferenceStartingDTO);
		if (conferenceStartingDTO.getConferenceCreatorId() == null) {
			conferenceStartingDTO.setConferenceCreatorId(creatorId);
		}
		return SUCCESS;
	}

	public ConferenceStartingDTO getConferenceStartingDTO() {
		return conferenceStartingDTO;
	}

	public void setConferenceStartingDTO(
			ConferenceStartingDTO conferenceStartingDTO) {
		this.conferenceStartingDTO = conferenceStartingDTO;
	}

	public String getUserInfoDtosJson() {
		return userInfoDtosJson;
	}

	public void setUserInfoDtosJson(String userInfoDtosJson) {
		this.userInfoDtosJson = userInfoDtosJson;
	}

}
